﻿using Dapper;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Senparc.CO2NET.Helpers;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using Newtonsoft.Json.Linq;
using Senparc.CO2NET.Extensions;


namespace ConsoleApp1
{
    internal class Program
    {
    
        public static string Kl_connectionString = "Server=127.0.0.1;Database=kl_activity;Uid=adkele;Pwd=liang123@usa.com;";
        public static string ex_connectionString = @"user id=sa;password=liang123@usa.com;initial catalog=PRE000DATExchange_Nexus2019;data source=dataserver";

        private static void Main(string[] args)
        {
          var o = new {name = "kong", age = 4.1, pay_fall = 4.1, now = DateTime.Now};
          var jo = JObject.FromObject(o);
          var jLoad = new JsonLoadSettings();

          var token = JToken.Parse("");
          token.Value<JObject>("").TryGetValue("", out var jj);
  





            var name=  jo.GetValue("name");
          var age = jo.Value<float>("age");
          var pay_fall = jo.GetValue("pay_fall");
          var null_valu1e = jo.Value<decimal>("pay_fall");
            var null_value = jo.GetValue("not__");



            var d = DateTime.Now;
            var ss = d.Ticks;
            var s = "234333" + DateTimeHelper.GetUnixDateTime(d).ToString();
            var ss2 = s.GetHashCode();

            var json = "   ['1' ,   2]";
           


            var sql2 = @"SELECT * FROM Users WHERE id = @id
        SELECT * FROM Items LEFT OUTER JOIN ItemLayouts ON Items.id = ItemLayouts.itemId WHERE userId = @id
        SELECT * FROM Collections LEFT OUTER JOIN CollectedItems ON Collections.id = CollectedItems.collectionId  WHERE userId = @id
        SELECT * FROM Tags LEFT OUTER JOIN TaggedItems ON Tags.id = TaggedItems.tagId  WHERE userId = @id";

            Console.WriteLine(sql2);
            Console.ReadLine();
            Console.WriteLine("Hello World!");
           // InsertImageDataTable();
        }

        private static void InsertImageDataTable()
        {
            const string sql = "UPDATE Exc_Document set DocumentData=@data, DocumentSize=@size,ContentType='image/jpg' WHERE DocumentID=@id";

            using (var connection = new SqlConnection(ex_connectionString))
            {
                var docIds = new List<int>
                {
                    1001574, 1001706, 1001787
            };

                for (int i = 0; i < 2; i++)
                {
                    var name = "church" + (i + 1) + ".jpg";
                    var path = @"C:\ChurchPictures\" + name;
                    var data = File.ReadAllBytes(path);
                    var size = data.Length;
                    var id = docIds[i];
                    connection.Execute(sql, new {  data,size, id });
                }
            }
        }

        private static void FillMusicDataTable()
        {
            var path = @"C:\gitee\Repos\kl-activity\vs-server\ConsoleApp1\music.json";
            var json = File.ReadAllText(path, Encoding.UTF8);

            var list = JsonConvert.DeserializeObject<List<jsonMusicTag>>(json);
            const string sqlTag = "insert into kl_activity_music_tag(id,tag_name) values(@tag_id,@tag_name)";
            const string sqlMusic = "insert into kl_activity_music(id,tag_id,name,path) values(@id,@tag_id,@name,@path)";
            using (var connection = new MySqlConnection(Kl_connectionString))
            {
                foreach (var listItem in list)
                {
                    connection.Execute(sqlTag, new { listItem.tag_id, listItem.tag_name });
                    foreach (var item in listItem.bgm_list)
                    {
                        connection.Execute(sqlMusic, new { item.id, listItem.tag_id, item.name, item.path });
                    }
                }
            }
        }

        private static void FillRegisterList()
        {
            var path = @"C:\gitee\Repos\kl-activity\vs-server\ConsoleApp1\registerList.json";
            var json = File.ReadAllText(path, Encoding.UTF8);
            var activity_id = "d10086e49c4ac005";

            var list = JsonConvert.DeserializeObject<List<jsonRegister>>(json);

            const string sql = "insert into kl_activity_register(activity_id,nick,avatar,createdAt) values(@activity_id,@nick,@avatar,@createdAt)";
            using (var connection = new MySqlConnection(Kl_connectionString))
            {
                foreach (var item in list)
                {
                    var nick = SerializerHelper.EncodeUnicode(item.nick);
                    connection.Execute(sql, new { activity_id, nick, item.avatar, item.createdAt });
                }
            }
        }
    }
}